package org.dfl.plugin.views.actions;

import org.dfl.core.data.Token;
import org.dfl.core.model.Place;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.dialogs.DialogsManager;
import org.dfl.plugin.views.debug.HistoriesTreeView;
import org.dfl.plugin.views.debug.TokensTreeView;
import org.dfl.plugin.views.debug.model.HistoryTreeElem;
import org.dfl.plugin.views.debug.model.TokenTreeElem;
import org.dfl.plugin.views.debug.model.TokensTreeViewRoot;
import org.dfl.plugin.views.debug.model.TreeElemFactory;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.ActionDelegate;

/**
 * action that provide adding new token. It's used in a TokensTreeView (see
 * mapping in plugin.xml file)
 * 
 * @author Piotr Wlodarczyk
 * 
 */
public class AddTokenAction extends ActionDelegate implements
		IViewActionDelegate {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		if (view != null && view.getCommonViewer().getInput() != null
				&& !view.getHistoryTreeElems().isEmpty()) {
			TokensTreeViewRoot root = (TokensTreeViewRoot) view
					.getCommonViewer().getInput();
			Place place = view.getHistoryTreeElems().iterator().next()
					.getPlace();
			Object data = TreeElemFactory.getEmptyInstance(null,
					place.getTypeContainer().getType(),true).getValue();

			
			if (root.getHistoryTreeElems().size() > 1
					&& !DialogsManager
							.openOkCancelDialog(
									"Confirm",
									"You have selected multiple histories.\n All tokens will be added to each of those histories.\n Are you sure to continue?")) {
				return;
			}
			HistoriesTreeView historiesTreeView = HistoriesTreeView.getView();
			TokensTreeView tokensTreeView = TokensTreeView.getView();
			// place listeners have to be removed before
			if (historiesTreeView != null)
				historiesTreeView.stopListenPlace();
			if (tokensTreeView != null)
				tokensTreeView.stopListenPlace();

			for (HistoryTreeElem historyTreeElem : view.getHistoryTreeElems()) {
				Token token = new Token(data, historyTreeElem.getHistory());
				synchronized (DflPlugin.class) {
					historyTreeElem.getTokens().add(token);
					place.addToken(token);
					root.addChild(new TokenTreeElem(root, token, place));
				}
			}
			// restore place listeners
			if (historiesTreeView != null)
				historiesTreeView.startListenPlace();
			if (tokensTreeView != null)
				tokensTreeView.startListenPlace();

			view.getCommonViewer().refresh();
		}
	}

	TokensTreeView view;

	public void init(IViewPart view) {
		if (view instanceof TokensTreeView)
			this.view = (TokensTreeView) view;
	}
}
